﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Xml;
using System.Windows.Forms;
using System.IO;
using AntikCompta.Data;

namespace AntikCompta
{
    internal class CouleurLotMng
    {
        private static CouleurLotMng mCouleurLotMng = null;

        public static void SauverCouleurs()
        {
            if( mCouleurLotMng != null )
                mCouleurLotMng.Sauver();
        }

        public static CouleurLotMng GetObject()
        {
            if (mCouleurLotMng == null)
                mCouleurLotMng = new CouleurLotMng();

            return mCouleurLotMng;
        }

        public static void ChargerCouleurs()
        {
            GetObject().Charger();
        }

        private Hashtable mRefColors;
        private CouleurLotMng()
        {
            mRefColors = new Hashtable();
            Charger();
        }

        public event DataChangedHandler ColorChanged;

        private bool GetColorFilePath(out String aFile)
        {
            aFile = "";
            String path;
            if (Tools.GetConfig("DernierFichier", out path) == false)
                return false;

            if (File.Exists(path) == false)
                return false;

            String filePath = Path.GetDirectoryName(path);
            aFile = Path.Combine(filePath, "couleurs.xml");

            return true;
        }

        private void Charger()
        {
            mRefColors.Clear();

            String filePath;
            if (!GetColorFilePath(out filePath))
                return;

            if (!File.Exists(filePath))
                return;

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            settings.IgnoreWhitespace = true;
            settings.IgnoreComments = true;
            XmlReader reader = XmlReader.Create(filePath, settings);
            ReadXml(reader);
        }

        private void Sauver()
        {
            String filePath;
            if (!GetColorFilePath(out filePath))
                return;

            XmlWriter writter = XmlWriter.Create(filePath);
            WriteXml(writter);
            writter.Flush();
            writter.Close();
        }

        public bool GetColor(String aRefLot, out Color aCouleur)
        {
            aCouleur = Color.Empty;
            if( mRefColors.Contains(aRefLot) == false )
                return false;

            aCouleur = (Color)mRefColors[aRefLot];

            return true;
        }

        public void SetColor(String aRefLot, Color aCouleur)
        {
            mRefColors[aRefLot] = aCouleur;

            if (ColorChanged != null)
                ColorChanged();
        }

        public void DelColor(String aRefLot)
        {
            if (mRefColors.Contains(aRefLot))
                mRefColors.Remove(aRefLot);

            if (ColorChanged != null)
                ColorChanged();
        }

        private bool WriteXml(System.Xml.XmlWriter writer)
        {
            if (mRefColors.Count <= 0)
                return false;

            // Write the root element 
            writer.WriteStartElement("dictionary");

            // Foreach object in this (as i am a Hashtable)
            foreach(object key in mRefColors.Keys)
            {
                Color value = (Color)mRefColors[key];
                // Write item, key and value
                writer.WriteStartElement("item");
                writer.WriteElementString("key", key.ToString()); 
                writer.WriteElementString("value", value.ToArgb() + "" );
                // write </item>
                writer.WriteEndElement();
             }
         
             // write </dictionary>
             writer.WriteEndElement();

             return true;
        }

        private void ReadXml(System.Xml.XmlReader reader)
        {
            // Start to use the reader.
            if (reader.Read() == false)
                return;

            // Read the first element i.e. root of this object
            reader.ReadStartElement("dictionary");

            if (reader.NodeType == XmlNodeType.None)
                return;

            // Read all elements
            while ( reader.NodeType != XmlNodeType.EndElement )
            {
                // parsing the item
                reader.ReadStartElement("item");

                // Parsing the key and value 
                string key = reader.ReadElementString("key");
                string value = reader.ReadElementString("value");
                // end reading the item.
                reader.ReadEndElement();
                reader.MoveToContent();

                // add the item
                int argb = Convert.ToInt32(value);
                mRefColors.Add(key, Color.FromArgb(argb));
            }

            // Extremely important to read the node to its end.
            // next call of the reader methods will crash if not called.
            reader.ReadEndElement();
        }
    }
}
